<?php

namespace app\company\controller;

use app\company\validate\PickingStoreValidate;
use app\mini\model\StoreOrderDetail;
use app\service\traits\Crud;
use app\company\model\PickingStore as PickingStoreModel;
use app\company\model\StoreOrder;
use app\company\model\Store;
use app\company\model\StoreUser;
use app\company\model\DeliverTask;
use app\company\model\DeliverDriver;

class PickingStore extends Base
{
    private PickingStoreModel $model;

    use Crud;

    public function initialize()
    {
        parent::initialize();
        $this->model         = new PickingStoreModel();
        $this->validateClass = PickingStoreValidate::class;
        $this->isGs          = true;
        $this->withSearch    = 'id,name,create_time';
        $this->append        = 'is_dow_text,is_print_text';
    }

    public function format($list): mixed
    {
        return $list->each(function ($item) {
            $item->dow_time   = setDates($item->dow_time);
            $item->print_time = setDates($item->print_time);
            $item->pick_time  = setDates($item->pick_time);
        });
    }

    public function detailFormat($info): mixed
    {
        //获取门店订单ID

        $date = date('Y-m-d', strtotime("-1 day", $info->pick_time));
        $orderIds     = StoreOrder::where('store_id', $info->store_id)->whereIn('order_status',[3,4])->whereDay('create_time', $date)->column('id');
//        $orderIds     = StoreOrder::where('id', $info->store_id)->column('id'); //测试用
        // $field        = 'goods_id,goods_rule_id,sum(quantity) quantity,min(money) money,sum(amount) amount,is_send';
        $field        = 'goods_id,goods_rule_id,quantity,money,amount,is_send';
        // 需要把商品拆开，不能再group了，要按照普通（pt）和赠送（send）拆成两个数组
        // $info->detail = StoreOrderDetail::whereIn("store_order_id", $orderIds)->field($field)->group('goods_rule_id')->with([
        $details = StoreOrderDetail::whereIn("store_order_id", $orderIds)->field($field)->with([
            'goods'      => getWith('id,name,no'),
            'goods_rule' => getWith('id,spec_name,sku')
        ])->select();
        $goodsData = [
            'pt' => [],
            'send' => [],
        ];
        foreach($details as $detail){
            $detail['is_send'] == 1 ? $goodsData['pt'][] = $detail->toArray() : $goodsData['send'][] = $detail->toArray();
        }
        $info->detail = $goodsData;
        //门店信息
        $info->store = Store::where('id', $info->store_id)->withTrashed()->field('name,address,salesman_id')->find();
        //客户名称
        $info->store_user = '';
        $info->store_user = StoreUser::where('store_id', $info->store_id)->field('real_name,mobile')->find();
        if($info->store_user) $info->store_user = $info->store_user->real_name.$info->store_user->mobile;

        //业务员
        $info->salesman = '';
        $info->salesman = StoreUser::where('id', $info->store->salesman_id)->field('real_name,mobile')->find();
        if($info->salesman) $info->salesman = $info->salesman->real_name.$info->salesman->mobile;
        //司机名称 //找到路线1
        $info->driver      = '';
        $deliver_task_id = StoreOrder::whereIn('id', $orderIds)->value('deliver_task_id');
        if ($deliver_task_id) {
            $deliver_driver_id = DeliverTask::where('id', $deliver_task_id)->value('deliver_driver_id');
            if ($deliver_driver_id){
                if($driver = DeliverDriver::where('id', $deliver_driver_id)->field('name,mobile')->find()){
                    $info->driver = $driver->name.$driver->mobile;
                }
            }
        }

        $info->dow_time   = setDates($info->dow_time);
        $info->print_time = setDates($info->print_time);
        $info->pick_time  = setDates($info->pick_time);
        return $info;
    }

}